/*
** heap.c: the implements of heap.h
** 
** Copyright (C) 2007-2008 SKY-MOBI AS.  All rights reserved.
**
** Create date: 2007-12-27 by wandonglin
**
** This file is part of the simple gui library.
** It may not be redistributed under any circumstances.
*/

#include "heap.h"
#include "debug.h"


HBLOCKHEAP GUIAPI
BlockHeap_Initialize(VOID* buffer, int buffersize, int blocksize)
{
	PBYTE pBlock = (PBYTE)buffer;

	SGL_ASSERT(0 == MOD(buffersize, blocksize) && 0 == MOD(blocksize, 4));
	/*
	if(0 != MOD(buffersize, blocksize) || 0 != MOD(blocksize, 4))
	{
		SGL_TRACE"BlockHeap_Initialize failed\n"));
		mrc_exit();
	}
	*/

	for(buffersize -= blocksize; pBlock - (PBYTE)buffer < buffersize ; pBlock += blocksize)
		*(Uint8**)pBlock = pBlock + blocksize;

	*(PBYTE*)pBlock = NULL;
	return (HBLOCKHEAP)buffer;
}


VOID* GUIAPI
BlockHeap_Alloc(HBLOCKHEAP* pHeap)
{
	VOID* pBlock = (VOID*)(*pHeap);

	if(pBlock)
		*pHeap =(HBLOCKHEAP)( *(PBYTE*)(*pHeap) );

	return pBlock;
}


VOID GUIAPI
BlockHeap_Free(HBLOCKHEAP* pHeap, VOID* pBlock)
{
	*(PBYTE*)pBlock = (PBYTE)(*pHeap);
	*pHeap = (HBLOCKHEAP)pBlock;
}

